//
//  ArtTableViewController.m
//  ArtLive
//
//  Created by weijingyun on 16/6/30.
//  Copyright © 2016年 LeeWong. All rights reserved.
//


#define WEAKSELF(weakSelf)  __weak __typeof(&*self)weakSelf = self;


#import "ArtTableViewController.h"
#import <Masonry.h>
@interface ArtTableViewController()

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ArtTableViewController

- (UITableViewStyle)tableViewStyle{
    return UITableViewStyleGrouped;
}

- (EArtTableViewRefreshType)refreshType{
    return EArtTableViewRefreshTypeTop | EArtTableViewRefreshTypeBottom;
}

- (void)configRefresh
{
    WEAKSELF(weakSelf)
    if (self.refreshType & EArtTableViewRefreshTypeTop) {
        
        [self.tableView addPullToRefreshWithActionHandler:^{
            [weakSelf insertRowAtTop];
        }];
    }
    
    if (self.refreshType & EArtTableViewRefreshTypeBottom) {
        
        [self.tableView addInfiniteScrollingWithActionHandler:^{
            [weakSelf insertRowAtBottom];
        }];
//        [self.tableView setShowsInfiniteScrolling:NO];
    }
    
}

- (void)insertRowAtTop {
    [self doRequestRefresh:YES];
}

- (void)insertRowAtBottom {
    [self doRequestRefresh:NO];
}

- (void)doRequestRefresh:(BOOL)aRefresh {
    NSLog(@"重写该方法实现刷新");
}

- (void)handleRetry {
    [self insertRowAtTop];
}

- (void)configViewDidLoadUI {
    
}

- (void)viewDidLoad{
    [super viewDidLoad];
    //    self.view.backgroundColor = [UIColor art_background];
    [self configViewDidLoadUI];
    [self configRefresh];
    if (self.navigationController) {
       
    }
    else{
       
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(self.view);
        }];

        
    }

}

- (void)dealloc{
    // 清空，避免之后有问题
    self.tableView = nil;
}

#pragma mark - 刷新收回的处理
- (void (^)(NSDictionary* dic))nextBlockRefresh:(BOOL)aRefresh reloadBlock:(void (^)(NSDictionary* dic))aBlock{
    __weak typeof(self) weakSelf = self;
    return ^(NSDictionary* dic){
        __strong typeof(weakSelf) self = weakSelf;
        [self sendNext:dic refresh:aRefresh reloadBlock:aBlock];
    };
}

- (void (^)(NSDictionary* dic))nextBlockRefresh:(BOOL)aRefresh{
    return [self nextBlockRefresh:aRefresh reloadBlock:nil];
}

- (void)sendNext:(NSDictionary *)aDic refresh:(BOOL)aRefresh {
    [self sendNext:aDic refresh:aRefresh reloadBlock:nil];
}

- (void)sendNext:(NSDictionary *)aDic refresh:(BOOL)aRefresh reloadBlock:(void (^)(NSDictionary* dic))aBlock{

   if (aDic == nil || aDic[kRACUIPackup]) {
    
        [self.tableView.infiniteScrollingView stopAnimating];
       
       return;
   }
    
}
    

#pragma mark - 懒加载
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.tableViewStyle];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:_tableView];
//        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//          make.edges.equalTo(self.view);
//        }];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.view);
//            make.top.equalTo(self.navigationController.navigationBar.mas_bottom);
        }];
    }
    return _tableView;
}

@end
